import {ActionSheet, AlbumView, Overlay} from 'teaset';
import React from 'react';
import {ActivityIndicator, BackHandler, Dimensions, StatusBar, ToastAndroid} from 'react-native';
const WIN_WIDTH = Dimensions.get('window').width;
const WIN_HEIGHT = Dimensions.get('window').height;
import FadeImage from "../components/FadeImage";
const modalViews = [];
const ModalViewUtil = {
    showFromBottom(view, containerStyle, onDisappearCompleted = null, onAppearCompleted = null) {
        return ModalViewUtil.showFromPosition(view, 'bottom', false, containerStyle, onDisappearCompleted, onAppearCompleted);
    },
    showFromPosition(view, position='bottom', modal = false, containerStyle, onDisappearCompleted = null, onAppearCompleted = null, props={}  ) {
        ModalViewUtil.overlayView = (
            <Overlay.PullView side={position} modal={modal} rootTransform={'none'}
                              onDisappearCompleted={()=>{
                                  onDisappearCompleted && onDisappearCompleted();
                                  Overlay.hide(ModalViewUtil.key);
                              }}
                              {...props}
                              containerStyle={containerStyle}
                              onAppearCompleted={onAppearCompleted}
                              ref={v => {
                                  if (v) {
                                      ModalViewUtil.overlayPullView = v;
                                      modalViews.push({ key:ModalViewUtil.key, element:  ModalViewUtil.overlayPullView});
                                  }
                              }}>
                {view}

            </Overlay.PullView>);
        ModalViewUtil.key = Overlay.show(ModalViewUtil.overlayView);
        return ModalViewUtil.key;
    },
    showFromTop(view, containerStyle, onDisappearCompleted = null, onAppearCompleted = null) {
        return ModalViewUtil.showFromPosition(view, 'top', false, containerStyle, onDisappearCompleted, onAppearCompleted);
    },
    showFromTopWithOverlayProps(view, containerStyle, props, onDisappearCompleted = null, onAppearCompleted = null) {
        return ModalViewUtil.showFromPosition(view, 'top', false, containerStyle, onDisappearCompleted, onAppearCompleted, props);
    },
    dismissBottomView() {
        if (modalViews.length > 0) {
            let key = modalViews.pop();
            key.element.close();
        }
    },
    dismissView() {
        if (modalViews.length > 0) {
            let key = modalViews.pop();
            key.element.close();
            Overlay.hide(key.key);
        }
    },
    showFromCenterWithOverlayProps(view, modal, props, onDisappearCompleted = null, onAppearCompleted = null) {
        return ModalViewUtil.showFromCenter(view, modal, {}, onDisappearCompleted, onAppearCompleted, props);
    },
    showFromCenter(view, modal = false, containerStyle, onDisappearCompleted = null, onAppearCompleted = null, props) {
        let overlayView = (
            <Overlay.PopView
                style={{alignItems: 'center', justifyContent: 'center'}}
                type={'zoomOut'}
                modal={modal}
                onDisappearCompleted={()=>{
                    onDisappearCompleted && onDisappearCompleted();
                    Overlay.hide(ModalViewUtil.key);
                }}
                {...props}
                containerStyle={containerStyle}
                onAppearCompleted={onAppearCompleted}
                ref={v => {
                    if (v) {
                        ModalViewUtil.overlayPullView = v;
                        modalViews.push({ key:ModalViewUtil.key, element:  ModalViewUtil.overlayPullView});
                    }
                }}>

                {view}
            </Overlay.PopView>);
        ModalViewUtil.key = Overlay.show(overlayView);
        return ModalViewUtil.key;
    },
    showImages(images, defaultIndex = 0, customBounds = {x: WIN_WIDTH/2, y: WIN_HEIGHT/2, width: 0, height: 0}) {
        let overlayView = (
            <Overlay.PopView
                style={{}}
                containerStyle={{flex: 1}}
                overlayOpacity={1}
                type='custom'
                customBounds={customBounds}
                ref={v => ModalViewUtil.fullImageView = v}
            >
                <AlbumView
                    style={{flex: 1}}
                    control={true}
                    images={images.map(value => {
                        return <FadeImage defaultSource={null}
                                          showLoading={true}
                                          source={{uri: value}}
                                          resizeMode={'contain'}
                                          style={{width: WIN_WIDTH, height: WIN_HEIGHT}}
                                          backgroundColor={'transparent'}/>;
                    })}
                    defaultIndex={defaultIndex}
                    onPress={() => ModalViewUtil.fullImageView && ModalViewUtil.fullImageView.close()}
                />
            </Overlay.PopView>
        );
        Overlay.show(overlayView);
    }
};

export default ModalViewUtil;
